package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hmdp.content.RedisConstants.FOLLOW_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserService userService;
    /**
     * 关注
     * @param id
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long id, Boolean isFollow) {
        //获取登入用户信息
        Long userId = UserHolder.getUser().getId();
        String key =FOLLOW_KEY + userId;
        //判断是否关注，没关注则新增关注对象
        if (BooleanUtil.isTrue(isFollow)){
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            boolean isSave = save(follow);
            if (isSave){
                stringRedisTemplate.opsForSet().add(key,id.toString());
            }
        }else {
            //已关注则，删除关注对象
            boolean isremove = remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", id));
            if (isremove){
                stringRedisTemplate.opsForSet().remove(key,id);
            }
        }
        return Result.ok();
    }

    /**
     * 查询是否关注
     * @param id
     * @return
     */
    @Override
    public Result isFollow(Long id) {
        //查询用户信息
        Long userId = UserHolder.getUser().getId();
        //查询是否关注
        Integer count = query().eq("user_id", userId).eq("follow_user_id", id).count();
//        boolean isFollow = (followList != null);
        return Result.ok(count>0);
        //返回结果
    }

    /**
     * 查询共同用户
     * @param id
     * @return
     */
    @Override
    public Result FollowCommons(Long id) {
        //先获取当前用户关注列表
        Long userId = UserHolder.getUser().getId();
        String key1 = FOLLOW_KEY+userId;
        //获取目标用户，
        String key2 = FOLLOW_KEY+id;
        //获取交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if (intersect == null || intersect.isEmpty()){
            //返回空，没有公共关注
            return Result.ok(Collections.emptyList());
        }
        //解析数据
        List<Long> followIdList = intersect.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        List<UserDTO> userDTOS = userService.listByIds(followIdList)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());

        return Result.ok(userDTOS);
    }
}
